<?php

    /**
    * This is the model class for table "user".
    *
    * The followings are the available columns in table 'user':
    * @property string $user_id
    * @property string $role
    *
    * The followings are the available model relations:
    * @property Image[] $images
    * @property Video[] $videos
    * @property Joke[] $jokes
    */
    class UserInfo extends CActiveRecord
    {
        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return User the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'user_info';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('user_id, role', 'required'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('user_id, role', 'safe', 'on'=>'search'),
            );
        }

        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(               
                'images' => array(self::HAS_MANY, 'Image', 'user_id'),
                'videos' => array(self::HAS_MANY, 'Video', 'user_id'),
                'jokes' => array(self::HAS_MANY, 'Joke', 'user_id'),
            );
        }

        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'user_id' => 'User ID',
                'role' => 'Role',
            );
        }

        /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
        public function search()
        {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('user_id',$this->id,true);
            $criteria->compare('role',$this->role,true);

            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }
        
        private $_model;
        public function getModel($user_id){
            if(!$this->_model)  $this->_model = $this->findByPk($user_id);   
            return $this->_model;        
        }
        


        public function getRoleData(){
            return array(
                'ADMIN' => Yii::t('app', 'Admin'),
                'MOD' => Yii::t('app', 'Mod'),
            );
        }



        public function getIsModOnly(){
            return in_array($this->role, array('MOD'));
        }
        public function getIsMod(){
            return in_array($this->role, array('MOD', 'ADMIN'));
        }
        public function getIsAdmin(){
            return in_array($this->role, array('ADMIN'));
        }


        public function getUrlLevel($level = NULL){
            if(is_null($level)){
                if($this->isAdmin){
                    $level = 0;
                }else{
                    $gbData = $this->gunboundLevelData;
                    $point = 0;
                    $points = array_reverse(array_keys($gbData));
                    foreach($points as $p){
                        if($p <= $this->point_all){
                            $point = $p;
                            break;
                        }
                    }
                    $level = $gbData[$point]['level']; 
                }
            }
            return "/files/img/gundbound_level/rank_{$level}.gif";
        }


        public function getGunboundLevelData(){
            return array(
                0 => array(
                    'level' => 1,
                    'name' => Yii::t('app', 'A Little Chick'),
                ),
                50 => array(
                    'level' => 2,
                    'name' => Yii::t('app', 'Wood Hammer'),
                ),
                100 => array(
                    'level' => 3,
                    'name' => Yii::t('app', 'Double Wood Hammer'),
                ),
                150 => array(
                    'level' => 4,
                    'name' => Yii::t('app', 'Stone Axe'),
                ),
                200 => array(
                    'level' => 5,
                    'name' => Yii::t('app', 'Double Stone Axe'),
                    'point' => 180,
                ),
                300 => array(
                    'level' => 6,
                    'name' => Yii::t('app', 'Metal Axe'),
                    'point' => 250,
                ),
                400 => array(
                    'level' => 7,
                    'name' => Yii::t('app', 'Double Metal Axe'),
                    'point' => 330,
                ),
                600 => array(
                    'level' => 8,
                    'name' => Yii::t('app', 'Silver Axe'),
                    'point' => 420,
                ),
                800 => array(
                    'level' => 9,
                    'name' => Yii::t('app', 'Double Silver Axe'),
                    'point' => 520,
                ),
                1100 => array(
                    'level' => 10,
                    'name' => Yii::t('app', 'Gold Axe'),
                    'point' => 630,
                ),
                1400 => array(
                    'level' => 11,
                    'name' => Yii::t('app', 'Double Gold Axe'),
                    'point' => 740,
                ),
                1800 => array(
                    'level' => 12,
                    'name' => Yii::t('app', 'Metal Double Sided Axe '),
                    'point' => 860
                ),
                2200 => array(
                    'level' => 13,
                    'name' => Yii::t('app', 'Metal Double Sided Axe +'),
                    'point' => 990,
                ),
                2700 => array(
                    'level' => 14,
                    'name' => Yii::t('app', 'Silver Double Sided Axe'),
                    'point' => 1130,
                ),
                3200 => array(
                    'level' => 15,
                    'name' => Yii::t('app', 'Silver Double Sided Axe +'),
                    'point' => 1280,
                ),
                3800 => array(
                    'level' => 16,
                    'name' => Yii::t('app', 'Gold Double Sided Axe'),
                    'point' => 1440,
                ),
                4400 => array(
                    'level' => 17,
                    'name' => Yii::t('app', 'Gold Double Sided Axe +'),
                    'point' => 1610,
                ),
                5100 => array(
                    'level' => 18,
                    'name' => Yii::t('app', 'Bronze Star'),
                    'point' => 1790,
                ),
                5900 => array(
                    'level' => 19,
                    'name' => Yii::t('app', 'Silver Star'),
                    'point' => 1980,
                ),
                6800 => array(
                    'level' => 20,
                    'name' => Yii::t('app', 'Gold Star'),
                    'point' => 2180,
                ),
                7800 => array(
                    'level' => 21,
                    'name' => Yii::t('app', 'Double Stars'),
                    'point' => 2500,
                ),
                9000 => array(
                    'level' => 22,
                    'name' => Yii::t('app', 'Triple Stars'),
                    'point' => 2900,
                ),
                10400 => array(
                    'level' => 23,
                    'name' => Yii::t('app', 'Violet Hole'),
                    'point' => 3400,
                ),
                12000 => array(
                    'level' => 24,
                    'name' => Yii::t('app', 'Sapphire Hole'),
                    'point' => 4000,
                ),
                13800 => array(
                    'level' => 25,
                    'name' => Yii::t('app', 'Red Ruby Hole'),
                    'point' => 4700,
                ),
                15800 => array(
                    'level' => 26,
                    'name' => Yii::t('app', 'Diamond Hole'),
                    'point' => 5500,
                ),
                20000 => array(
                    'level' => 27,
                    'name' => Yii::t('app', 'Blue Dragon'),
                    'point' => 6400,
                ),
                25000 => array(
                    'level' => 28,
                    'name' => Yii::t('app', 'Red Dragon'),
                    'point' => 7400,
                ),
                30000 => array(
                    'level' => 29,
                    'name' => Yii::t('app', 'Silver Dragon'),
                    'point' => 8500,
                ),
                //            30 => array(
                //                'level' => 30,
                //                'name' => Yii::t('app', 'Blue Knight'),
                //                'point' => 10000,
                //            ),
                //            31 => array(
                //                'level' => 31,
                //                'name' => Yii::t('app', 'Red Knight'),
                //                'point' => 15000,
                //            ),
                //            32 => array(
                //                'level' => 32,
                //                'name' => Yii::t('app', 'Silver Knight'),
                //                'point' => 20000,
                //            ),
                //            33 => array(
                //                'level' => 33,
                //                'name' => Yii::t('app', 'Blue Phoenix'),
                //                'point' => 30000,
                //            ),
                //            34 => array(
                //                'level' => 34,
                //                'name' => Yii::t('app', 'Red Phoenix'),
                //                'point' => 40000,
                //            ),
                //            35 => array(
                //                'level' => 35,
                //                'name' => Yii::t('app', 'Silver Phoenix'),
                //                'point' => 50000,
                //            ),
                //            36 => array(
                //                'level' => 36,
                //                'name' => Yii::t('app', 'Bronze Medal'),
                //                'point' => 60000,
                //            ),
                //            37 => array(
                //                'level' => 37,
                //                'name' => Yii::t('app', 'Silver Medal'),
                //                'point' => 70000,
                //            ),
                //            38 => array(
                //                'level' => 38,
                //                'name' => Yii::t('app', 'Gold Medal'),
                //                'point' => 80000,
                //            ),
                //            39 => array(
                //                'level' => 39,
                //                'name' => Yii::t('app', 'Bronze Crown'),
                //                'point' => 90000,
                //            ),
                //            40 => array(
                //                'level' => 40,
                //                'name' => Yii::t('app', 'Silver Crown'),
                //                'point' => 100000,
                //            ),
                //            41 => array(
                //                'level' => 41,
                //                'name' => Yii::t('app', 'Gold Crown'),
                //                'point' => 120000,
                //            ),
                //            42 => array(
                //                'name' => Yii::t('app', 'VIP'),
                //                'point' => 11,
                //            ),
                //            43 => array(
                //                'name' => Yii::t('app', 'Bronze Trhophy'),
                //                'point' => 11,
                //            ),
                //            44 => array(
                //                'name' => Yii::t('app', 'Silver Trophy'),
                //                'point' => 11,
                //            ),
                //            45 => array(
                //                'name' => Yii::t('app', 'Gold Trophy'),
                //                'point' => 11,
                //            ),
            );
        }


        public function getRandomUserId(){
            $user_ids = array(1,1,1,1,6,6,7,7,7,7,11,11,22,22,22,23);
            return $user_ids[array_rand($user_ids, 1)];
        } 
}
